<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\CarInfoServices;
use think\facade\App;

/**
 * Class CarInfo
 * @package app\adminapi\controller\v1\shendeng
 */
class CarInfo extends AuthController
{
    public function __construct(App $app,CarInfoServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }
    /**
     * 列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(){
        $where = $this->request->getMore([
            ['is_del',0],
            ['keyword',''],
            ['status',''],
            ['type_info_id',''],
            ['place_id',''],
            ['car_status',''],
            ['sxj_status',''],
        ]);
        $field = '*';
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['place_id'] = $admin['store_id'];
        return app('json')->success($this->services->getList($where,$field,true));
    }

    /**
     * 添加/修改
     * @return mixed
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
            ['car_no', ''],
            ['run_km', ''],
            ['upkeep_km', ''],
            ['upkeep_day', ''],
            ['last_upkeep_date', ''],
            ['last_upkeep_km', ''],
            ['check_date', ''],
            ['insurance_date', ''],
            ['commercial_insurance_date', ''],
            ['type_id', 0],
            ['status', 1],
            ['img', ''],
        ]);
        if(!$data['car_no']) return app('json')->fail('请输入车牌号');
        if(!$data['type_id']) return app('json')->fail('请选择车型');
        $res = $this->services->save((int)$data['id'],$data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delInfo((int)$data['id']));
    }

    /**
     * 显示/隐藏
     * @param string $status
     * @param string $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setShow(string $status = '',string $id = ''){
        if ($status == '' || $id == '') return app('json')->fail('缺少参数');
        return app('json')->success($this->services->setShow((int)$id, (int)$status));
    }

    /**
     * 车辆信息批量删除
     * @return mixed
     */
    public function infoDelete(){
        $data = $this->request->getMore([
            ['ids',[]]
        ]);
        return app('json')->success($this->services->infoDelete($data['ids']));
    }

    /**
     * 获取所有车辆信息
     * @return mixed
     */
    public function getCarInfoAll(){
        return app('json')->success($this->services->getCarInfoAll());
    }
}
